package yawning.mew.cat;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationEvent;

public class EventMeleeSwing extends SimulationEvent {
	CatModelSimState mState;
	double mFurySwipesCooldownAt = 0.0;
	
	EventMeleeSwing(CatModelSimState state) {
		super(state.mSimUtil);
		mState = state;
		schedule(0.0);
	}

	@Override
	public void onExpire()  {
		schedule(Double.MAX_VALUE, mState.mModel.getMeleeInterval());
	}

	@Override
	public void onTick() {
		AttackResult result = mState.mModel.getWhiteResult();

		if (mState.mDebug) mState.DBGF("White Swing (%s)", result);

		if (result == AttackResult.MISS) {
			mState.nrWhiteMiss++;
		} else if (result == AttackResult.DODGE) {
			mState.nrWhiteDodge++;
		} else {
			double damage = mState.mModel.getWeaponDamage();

			if (result == AttackResult.GLANCE) {
				mState.nrWhiteGlance++;
				damage *= 0.75;
				mState.mSimUtil.triggerProc(Trigger.WHITE);
			} else if (result == AttackResult.CRIT) {
				mState.nrWhiteCrit++;
				damage *= mState.mModel.multCrit;
				mState.mSimUtil.triggerProc(Trigger.WHITECRIT);
			} else {
				mState.nrWhiteHit++;
				mState.mSimUtil.triggerProc(Trigger.WHITE);
			}

			// Build #12759, SR is auto attack only.
			damage *= mState.savageRoarEv.isUp() ? mState.mModel.multRoar : 1;
			damage *= mState.getDirectDamageMult();
			mState.whiteDamage += Math.round(damage);

			if (mState.mDebug) mState.DBGF("  Damage: %d", Math.round(damage));

			mState.oocEv.run();

			if (mFurySwipesCooldownAt <= mState.mSimUtil.getSimTime()) {
				if (mState.mSimUtil.getRandom().nextBoolean(mState.mModel.pFurySwipes)) {
					result = mState.mModel.getYellowResult();

					if (mState.mDebug) mState.DBGF("Fury Swipes (%s)", result);

					if (result == AttackResult.MISS) {
						mState.nrFurySwipesMiss++;
					} else if (result == AttackResult.DODGE) {
						mState.nrFurySwipesDodge++;
					} else {
						damage = mState.mModel.getWeaponDamage() * mState.mModel.multFurySwipes;

						if (result == AttackResult.CRIT) {
							mState.nrFurySwipesCrit++;
							damage *= mState.mModel.multCrit;
							mState.mSimUtil.triggerProc(Trigger.YELLOWCRIT);
						} else {
							mState.nrFurySwipesHit++;
							mState.mSimUtil.triggerProc(Trigger.YELLOW);
						}

						damage *= mState.getDirectDamageMult();
						mState.furySwipesDamage += Math.round(damage);

						if (mState.mDebug) mState.DBGF("  Damage: %d", Math.round(damage));
					}

					mFurySwipesCooldownAt = mState.mSimUtil.getSimTime() + mState.mModel.furySwipesCDDuration;
				}
			}
		}
		setTickInterval(mState.mModel.getMeleeInterval());
	}
}
